<template>
  <el-header class="navbar">
    <div class="logo-container">
      <h1 class="logo">新闻资讯</h1>
    </div>
    
    <el-menu 
      :default-active="activeIndex" 
      class="nav-menu" 
      mode="horizontal"
      background-color="#304156"
      text-color="#fff"
      active-text-color="#ffd04b"
    >
      <el-menu-item index="/">首页</el-menu-item>
      <el-menu-item index="/category/1">国内新闻</el-menu-item>
      <el-menu-item index="/category/2">国际新闻</el-menu-item>
      <el-menu-item index="/category/3">科技新闻</el-menu-item>
      <el-menu-item index="/category/4">娱乐新闻</el-menu-item>
      <el-menu-item index="/category/5">体育新闻</el-menu-item>
    </el-menu>
    
    <div class="user-actions">
      <el-button v-if="!isLoggedIn" type="primary" @click="goToLogin">登录</el-button>
      <el-button v-if="!isLoggedIn" @click="goToRegister">注册</el-button>
      
      <el-dropdown v-else @command="handleCommand">
        <span class="dropdown-link">
          <el-avatar :size="36" :src="userAvatar" />&nbsp;
          <span>{{ username }}</span>
          <el-icon class="el-icon--right"><CaretBottom /></el-icon>
        </span>
        
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="profile">个人中心</el-dropdown-item>
            <el-dropdown-item command="my-articles">我的文章</el-dropdown-item>
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </el-header>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { CaretBottom } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/userStore'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 当前激活的菜单索引
const activeIndex = computed(() => {
  const path = route.path
  if (path === '/') return '/'
  if (path.startsWith('/category/')) {
    const categoryId = path.split('/')[2]
    return `/category/${categoryId}`
  }
  return path
})

// 计算是否已登录
const isLoggedIn = computed(() => userStore.isLoggedIn)

// 计算用户名
const username = computed(() => userStore.user?.username || '用户')

// 计算用户头像
const userAvatar = computed(() => {
  const initials = username.value.charAt(0)?.toUpperCase() || 'U'
  return `https://picsum.photos/100/100?random=${initials}`
})

// 前往登录页面
const goToLogin = () => {
  router.push('/login')
}

// 前往注册页面
const goToRegister = () => {
  router.push('/register')
}

// 处理下拉菜单命令
const handleCommand = (command: string) => {
  switch (command) {
    case 'profile':
      router.push('/user/profile')
      break
    case 'my-articles':
      router.push('/user/articles')
      break
    case 'logout':
      userStore.logout()
      ElMessage.success('退出登录成功')
      router.push('/')
      break
    default:
      break
  }
}
</script>

<style scoped>
.navbar {
  background-color: #304156;
  color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 40px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  z-index: 100;
}

.logo-container {
  display: flex;
  align-items: center;
}

.logo {
  font-size: 24px;
  font-weight: 700;
  color: #fff;
  margin: 0;
}

.nav-menu {
  border-bottom: none;
  flex: 1;
  max-width: 600px;
  margin: 0 40px;
}

.user-actions {
  display: flex;
  align-items: center;
}

.el-dropdown .dropdown-link {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.el-dropdown .el-avatar {
  margin-right: 10px;
}

@media (max-width: 768px) {
  .navbar {
    padding: 0 20px;
  }
  
  .nav-menu {
    display: none;
  }
  
  .logo {
    font-size: 20px;
  }
}
</style>